package com.konne.nightmare.FastPublicOpinion.ui.information.activity;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.coorchice.library.SuperTextView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpActivity;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ExamAnalysisFrag;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ExamDetailsFrag;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ExamReviewFrag;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ExamSettingFrag;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.widget.QJActionBar;

import cn.jzvd.Jzvd;

/**
 * Create by ws.
 * on Data:2022/2/16  Time:上午 10:37.
 * description: 考试.
 */
public class ExamAct extends BaseMvpActivity {
    public static void start(int type){
        Intent intent=new Intent(ActivityUtils.getTopActivity(),ExamAct.class);
        intent.putExtra("type",type);
        ActivityUtils.startActivity(intent);
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return new BaseMvpPresenter();
    }

    @Override
    public int getLayout() {
        return R.layout.act_exam;
    }

    @Override
    public void initView() {
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this,false);
        int type=getIntent().getIntExtra("type",0);
        SuperTextView stv_state=findViewById(R.id.stv_state);
        switch (type){
            case 0://未开始
                stv_state.setText("未开始");
                stv_state.setSolid(0xffffffff);
                stv_state.setTextColor(0xff3396FE);
                break;
            case 1://考试中
                stv_state.setText("进行中");
                stv_state.setSolid(0xff3396FE);
                stv_state.setTextColor(0xffffffff);
                break;

        }
        initTop();
        TabLayout tabLayout=findViewById(R.id.tl_tab);
        for (int i = 0; i < titles.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        ViewPager viewPager=findViewById(R.id.viewpager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                BaseMvpFragment fragment=null;
                switch (position){
                    case 0:
                        fragment= ExamDetailsFrag.getInstance();
                        break;
                    case 1:
                        fragment= ExamSettingFrag.getInstance(type);
                        break;
                    case 2:
                        fragment= ExamReviewFrag.getInstance(type);
                        break;
                    case 3:
                        fragment= ExamAnalysisFrag.getInstance();
                        break;
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return titles.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        viewPager.setOffscreenPageLimit(titles.length);
        tabLayout.setupWithViewPager(viewPager);
    }
    private String[] titles={"考试详情","考试设置","批阅考试","考试分析"};
    private void initTop() {

        QJActionBar actionBar=findViewById(R.id.action_bar);
        ImageView actionRight=actionBar.getImgView(R.drawable.ic_action_set);
        actionRight.setImageTintList( ColorStateList.valueOf(0xffffffff));

        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) actionRight.getLayoutParams();
        params.rightMargin=SizeUtils.dp2px(15);
        actionRight.setLayoutParams(params);
        actionBar.getRightViewGroup().addView(actionRight);
        actionBar.getViewBg().setAlpha(0);
        ImageView actionBack=actionBar.setBackImg();
        actionBack.setImageTintList( ColorStateList.valueOf(0xffffffff));
        TextView actionTitle=actionBar.setTitle("考试");
        actionTitle.setTextColor(Color.WHITE);

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getLayoutParams().height = SizeUtils.dp2px(90) + BarUtils.getStatusBarHeight();

        AppBarLayout appBar=findViewById(R.id.appBar);
        int max = SizeUtils.dp2px(250);
        appBar.setTargetElevation(0);
        appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                float f = (float) Math.abs(i) / (float) max;
                actionBar.getViewBg().setAlpha(f);
                if (f > 0.5f) {
                    actionTitle.setTextColor(Color.BLACK);
                    actionBack.setImageTintList( ColorStateList.valueOf(0xff3396FE));
                    actionRight.setImageTintList( ColorStateList.valueOf(0xff3396FE));
                    BarUtils.setStatusBarLightMode(ExamAct.this,true);
                } else {
                    actionTitle.setTextColor(Color.WHITE);
                    actionRight.setImageTintList( ColorStateList.valueOf(0xffffffff));
                    actionBack.setImageTintList( ColorStateList.valueOf(0xffffffff));
                    BarUtils.setStatusBarLightMode(ExamAct.this,false);
                }
            }
        });

    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }
}
